package com.ml.structure.linkedlist.josephu;

public class Josephu {
	public ChildNode first = null;
	
	public void add(ChildNode childNode){
		if(first==null){
			first =childNode;
			first.setNext(first);
			return;
		}
		ChildNode temp=first;
		while(true){
			if(temp.getNext()==first){
				temp.setNext(childNode);
				childNode.setNext(first);
				break;
			}
			temp=temp.getNext();
		}
	}
	
	//显示链表
	public void show(){
		if(first==null){
			System.out.println("链表为空");
		}
		ChildNode temp=first;
		while(true){
			if(temp.getNext()==first){
				System.out.println(temp);
				break;
			}
			System.out.println(temp);
			temp=temp.getNext();
		}
	}
	//删除节点
	public void delete(ChildNode childNode){
		ChildNode temp=first;
		if(first==null){
			System.out.println("链表为空");
			return;
		}
		if(first.getNo()==childNode.getNo()&&first.getNext()==first){
			first=null;
			return;
		}
		//删除头节点
		if(first.getNo()==childNode.getNo()){
			while(true){
				if(temp.getNext()==first){
					first=first.getNext();
					temp.setNext(first);
					break;
				}
				temp=temp.getNext();
			}
			return;
		}
		while (true) {
			if (temp.getNext().getNo() == childNode.getNo() /*&& temp.getNext().getNext() == null*/) {
				temp.setNext(temp.getNext().getNext());
				break;
			}/* else if (temp.getNext().getNo() == childNode.getNo() && temp.getNext().getNext() != null) {
				temp.setNext(temp.getNext().getNext());
				break;
			}*/
			temp = temp.getNext();
		}
	}
	//实现报数弹出，
	public void popByIndex(){
		int m=2;
		ChildNode temp=first;
		while(true){
			if(temp.getNext()==first){
				System.out.println(temp);
			}
		}
	}
	
	
	
}
